/**
 * Created by admin on 2017/3/17.
 */
define(['app'], function(app) {
    app.register.controller('forgetPwd', function($scope, $http, $state) {
        $scope.tipModal = {
            timeout:false,
            tips:false,
            message:'',
            phonenumber:'',
            name:'',
            password:''
        };
        $scope.forget = true;//忘记密码界面显示
        $scope.getmsg =true;//获取验证码字样显示
        $scope.blur=function(){//失焦事件
            var exp = /^1[3|4|5|7|8][0-9]{9}$/;
            if(!$scope.phone) {//没有输入账号
                $('#btn-getmsg').removeClass('active');
                $scope.tipModal.message = '账号为空，请输入账号';
                $scope.tipModal.tips = true;
                return false;
            }
            if(!exp.test($scope.phone)){
                $scope.tipModal.message = '您输入的号码格式有误';
                $scope.tipModal.tips = true;
                return false;
            }
        };
        $scope.keyup = function() {//当按钮被松开时，发生 keyup 事件,它发生在当前获得焦点的元素上
            $('#btn-getmsg').removeClass('active');
            var len = $scope.phone.length;
            var exp=/^1[3|4|5|7|8][0-9]{9}$/;//验证账号正则
            if(!$scope.phone){
                $('#btn-getmsg').removeClass('active');
                $scope.tipModal.message = '账号为空，请输入账号';
                $scope.tipModal.tips = true;
                return false;
            }
            if(len<11){
                $('#btn-getmsg').removeClass('active');
                $scope.tipModal.tips = false;
                return false;
            }
            if(len==11&&exp.test($scope.phone)==false){//账号为11位，但不是账号
                $('#btn-getmsg').removeClass('active');
                $scope.tipModal.message = '您输入的号码格式有误';
                $scope.tipModal.tips = true;
            }else{
                $scope.tipModal.tips = false;
                $('#btn-getmsg').addClass('active');//获取验证码按钮变亮
            }
        };
        $scope.getmessage = function() {//点击获取验证码按钮
            var len = $scope.phone.length;
            var exp=/^1[3|4|5|7|8][0-9]{9}$/;//验证账号正则
            if(len == 11&&exp.test($scope.phone)==true) {//账号输入正确
                $scope.tipModal.tips = false;
                $('#btn-getmsg').addClass('active');//获取验证码按钮变亮
                $http({//发送请求
                    url: app._baseUrl + '/Pub/Public/updatePwdSend',//请求：登录注册-验证码发送接口
                    method: 'POST',
                    data: {
                        phone: $scope.phone
                        // type:2//类型2，忘记密码验证码
                    },
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    }
                }).success(function(res) {
                    if(res.success) {//验证码已发送
                        $scope.getmsg = false;//获取验证码字样隐藏
                        $scope.tipModal.timeout = true;//读秒显示
                        $scope.sec = 59;//59秒倒计时
                        var interval = setInterval(function() {//设置定时器
                            if($scope.sec > 0) {
                                $scope.sec--;
                            } else {
                                clearInterval(interval);
                                $scope.getmsg = true;//获取验证码字样显示
                                $scope.tipModal.timeout = false;//读秒隐藏
                            }
                            $scope.$digest();//脏数据检验
                        }, 1000);

                    } else {//验证码验证错误
                        $scope.tipModal.message = res.message;
                        $scope.tipModal.tips = true;
                    }

                })
            }
        };
        $scope.next = function(phone) {//给按钮下一步添加点击事件
            var mobile = $.trim($('.register-wrap input[name="mobile"]').val()),//账号
                message = $.trim($('.register-wrap-2 input[name="message"]').val());//	验证码
            $scope.yan_zm = message;
            if(!mobile || !message) {//账号或验证码不全
                $scope.tipModal.message = '账号和验证码不能为空';
                $scope.tipModal.tips = true;
                return false;
            }
            $http({
                url: app._baseUrl + '/Pub/Public/verify',//请求：登录验证-验证码验证接口
                method: 'POST',
                data: {
                    verifyCode: message,
                    phone: mobile
                },
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                }
            }).success(function(res) {//接口请求成功
                if(res.success) {//验证码发送成功
                    $scope.tipModal.phonenumber = mobile;//将账号赋给全局变量，保存起来，找回密码时用
                    $scope.forget = false;//忘记密码页面隐藏
                    $scope.gonext = true;//下一步设置界面显示
                    $scope.tipModal.tips = false;
                } else {//验证码发送错误
                    $scope.tipModal.message = '验证码错误，请重新输入';
                    $scope.tipModal.tips = true;
                }
            }).error(function() {//接口请求失败
                $scope.tipModal.message = '服务器错误，请稍后再试';
                $scope.tipModal.tips = true;
                return false;
            });

        };
        $scope.reged = function() {//完成添加点击事件
            if(!$scope.tipModal.name || !$scope.tipModal.password) {//密码或新密码为空
                $scope.tipModal.message = '密码和新密码不能为空';
                $scope.tipModal.tips = true;
                return false;
            }
            if($scope.tipModal.name.length<6){//密码长度小于6位
                $scope.tipModal.message = '密码长度不得小于六位';
                $scope.tipModal.tips = true;
                return false;
            }
            if($scope.tipModal.name!=$scope.tipModal.password){
                $scope.tipModal.message = '两次密码不一致';
                $scope.tipModal.tips = true;
                return false;
            }
            if($scope.tipModal.name.length>=6&&$scope.tipModal.name.length<=12&&$scope.tipModal.name==$scope.tipModal.password){//密码长度在6-12之间,且两次密码输入一致
                $http({
                    url: app._baseUrl + '/Pub/Public/findPwd',//请求：登录注册-修改密码接口
                    method: 'POST',
                    data: {
                        phone:$scope.tipModal.phonenumber,//账号
                        password:$scope.tipModal.name,//密码
                        // type:1
                        verifyCode:$scope.yan_zm
                    },
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    }
                }).success(function(res) {
                    if(res.success) {//数据判断正确
                        $scope.gonext = false;//本页面隐藏
                        $scope.success = true;//注册成功页面显示
                    } else {//发送数据错误
                        $scope.gonext = false;//本页面隐藏
                        $scope.error = true;//错误页面显示
                        $scope.tipModal.tips = false;
                    }
                }).error(function(res, header, config, status) {//请求接口错误
                    $scope.tipModal.message = '服务器错误，请稍后再试';
                    $scope.tipModal.tips = true;
                    return false;
                });
            }

        };
        $scope.gologin=function(){//回到登录页面一个点击事件
            var url = $state.href('access.login');//跳转到登录页面
            window.location.href=url;
        };
        $scope.goregedit=function(){//再试一次添加点击事件
            $scope.error=false;//错误页面隐藏
            $scope.forget=true;//忘记密码页面显示
            $scope.phone = '';
            $scope.message = '';
            $scope.getmsg =true;
            $scope.tipModal.timeout = false;
        };

    });
});